﻿using ChestPainCenterSystem.Areas.ChestPainCenter.Models;
using ChestPainCenterSystem.Core;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace ChestPainCenterSystem.Code.Bll
{
    public class AppCaseConfigManager
    {
        /// <summary>
        /// 获取质控列表（所有）
        /// </summary>
        /// <returns></returns>
        public static OperateResult GetData(string Id = "")
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                string NameSelect = @"use ThreeCenterManage; 
                            SELECT  * FROM dbo.Records_APP_Config 
                            WHERE State<>2 ";
                if (!string.IsNullOrEmpty(Id))
                {
                    NameSelect += @" and Id=" + Id;
                }
                NameSelect += @" ORDER BY Control_Order ASC ";
                DataSet NameList = SQLDLL.SqlHelper.ExecuteDataset(NameSelect, System.Data.CommandType.Text);


                if (NameList != null && NameList.Tables != null && NameList.Tables.Count > 0)
                {
                    DataTable dt = NameList.Tables[0];
                    resultInfo.Body = dt;
                    resultInfo.Result = 1;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = "查询失败";
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }
        /// <summary>
        /// 保存质控项
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="QualityPointTitle"></param>
        /// <param name="ComparePointId"></param>
        /// <param name="OverTime"></param>
        /// <param name="Sort"></param>
        /// <param name="Status"></param>
        /// <param name="SetUserId"></param>
        /// <returns></returns>
        public static OperateResult SaveData(AppCaseConfig qp)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                string NameSelect = string.Empty;
                if (qp.Id != "0" && !string.IsNullOrEmpty(qp.Id))
                {
                    NameSelect = @"use ThreeCenterManage; 
                            update [Records_APP_Config] 
                            set Name = '" + qp.Name + @",'
                                Type = '" + qp.Type + @",'
                                TCId = '" + qp.TCId + @",'
                                Table_Name = '" + qp.Table_Name + @",'
                                Column_Name = '" + qp.Column_Name + @",'
                                State = '" + qp.State + @",'
                                User_Control = '" + qp.User_Control + @",'
                                Other_Constraint = '" + qp.Other_Constraint + @",'
                                Power_UserID = '" + qp.Power_UserID + @",'
                                Control_Order = '" + qp.Control_Order + @",'
                                User_Control_Father = '" + qp.User_Control_Father + @",'
                                Control_Label = '" + qp.Control_Label + @"'
                            where Id=" + qp.Id;
                }
                else
                {
                    NameSelect = @"use ThreeCenterManage; 
                        INSERT INTO dbo.Records_APP_Config
                        (
                            Name,
                            Type,
                            TCId,
                            Table_Name,
                            Column_Name,
                            State,
                            User_Control,
                            Other_Constraint,
                            Power_UserID,
                            Control_Order,
                            User_Control_Father,
                            Control_Label
                        )
                        VALUES
                        (   N'" + qp.Name + @"', 
                            N'" + qp.Type + @"', 
                            N'" + qp.TCId + @"', 
                            N'" + qp.Table_Name + @"', 
                            N'" + qp.Column_Name + @"', 
                            N'" + qp.State + @"', 
                            N'" + qp.User_Control + @"', 
                            N'" + qp.Other_Constraint + @"', 
                            N'" + qp.Power_UserID + @"', 
                            N'" + qp.Control_Order + @"', 
                            N'" + qp.User_Control_Father + @"', 
                            N'" + qp.Control_Label       + @"'
                            )";
                }
                int rst = SQLDLL.SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, NameSelect);
                if (rst > 0)
                {
                    resultInfo.Result = 1;
                    resultInfo.Body = "保存成功";
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = "保存失败";
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }
        /// <summary>
        /// 状态更新
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public static OperateResult UpdateStatus(string Id, string Status)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                string NameSelect = @"use ThreeCenterManage; 
                            update [AppCaseConfig] set Status =  " + Status + " where Id=" + Id;
                int rst = SQLDLL.SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, NameSelect);


                if (rst > 0)
                {
                    resultInfo.Result = 1;
                    resultInfo.Body = "保存成功";
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = "保存失败";
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }
    }
}